/*
*  Copyright 2019-2023 CY
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.xiao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import me.zhengjie.modules.xiao.domain.CgCluetable;
import me.zhengjie.modules.xiao.domain.CgCustomertable;
import me.zhengjie.modules.xiao.domain.dto.CgCusExcelDTO;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.xiao.service.CgCustomertableService;
import me.zhengjie.modules.xiao.domain.vo.CgCustomertableQueryCriteria;
import me.zhengjie.modules.xiao.mapper.CgCustomertableMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author cg
* @date 2024-06-19
**/
@Service
@RequiredArgsConstructor
public class CgCustomertableServiceImpl extends ServiceImpl<CgCustomertableMapper, CgCustomertable> implements CgCustomertableService {

    private final CgCustomertableMapper cgCustomertableMapper;

    @Override
    public PageResult<CgCustomertable> queryAll(CgCustomertableQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(cgCustomertableMapper.findAll(criteria, page));
    }

    @Override
    public List<CgCustomertable> queryAll(CgCustomertableQueryCriteria criteria){
        return cgCustomertableMapper.findAll(criteria);
    }

    @Override
    public Integer telCount(String phone) {
        return cgCustomertableMapper.telCount(phone);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(CgCustomertable resources) {
        Integer count = cgCustomertableMapper.telCount(resources.getCustomerTel());
        if(count>0){
            throw new RuntimeException("手机号已存在");
        }
        resources.setId(null);
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CgCustomertable resources) {
        CgCustomertable cgCustomertable = getById(resources.getId());
        String messageStatus = resources.getMessageStatus();
        if(resources.getDeptId()!=cgCustomertable.getDeptId()){
            resources.setMessageStatus("1");
        }
        cgCustomertable.copy(resources);
        saveOrUpdate(cgCustomertable);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<CgCustomertable> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CgCustomertable cgCustomertable : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("客户名称", cgCustomertable.getCustomerName());
            map.put("客户电话", cgCustomertable.getCustomerTel());
            map.put("客户类型：客户/公海客户", cgCustomertable.getCustomerFlag());
            map.put("状态：潜在/洽谈", cgCustomertable.getCustomerStatus());
            map.put("备注", cgCustomertable.getRemark());
            map.put("客户地址", cgCustomertable.getCustomerAddress());
            map.put("部门ID", cgCustomertable.getDeptId());
            map.put("业务公司", cgCustomertable.getDeptName());
            map.put("创建者", cgCustomertable.getCreateBy());
            map.put("更新者", cgCustomertable.getUpdateBy());
            map.put("创建日期", cgCustomertable.getCreateTime());
            map.put("更新时间", cgCustomertable.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void deleteByPhone(String phone) {
        LambdaQueryWrapper<CgCustomertable> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CgCustomertable::getCustomerTel, phone);
        cgCustomertableMapper.delete(queryWrapper);
    }


    @Override
    public List<CgCusExcelDTO> queryExcel(CgCustomertableQueryCriteria criteria){
        return cgCustomertableMapper.queryExcel(criteria);
    }
}